chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  if (changeInfo.status === 'complete' && tab.url) {
    chrome.bookmarks.search({ url: tab.url }, (bookmarks) => {
      if (bookmarks.length > 0) {
        chrome.storage.sync.get(['visitCounts'], (result) => {
          let visitCounts = result.visitCounts || {};
          visitCounts[tab.url] = (visitCounts[tab.url] || 0) + 1;
          chrome.storage.sync.set({ visitCounts }, () => {
            // 检查存储使用情况
            chrome.storage.sync.getBytesInUse(null, (bytesInUse) => {
              const QUOTA_BYTES = chrome.storage.sync.QUOTA_BYTES;
              const THRESHOLD = QUOTA_BYTES * 0.9; // 90% of the storage limit
              if (bytesInUse >= THRESHOLD) {
                chrome.notifications.create({
                  type: 'basic',
                  iconUrl: 'icons/icon128.png',
                  title: '存储空间警告',
                  message: `存储空间即将满，已使用 ${bytesInUse} 字节，总容量 ${QUOTA_BYTES} 字节。`
                });
              }
            });
          });
        });
      }
    });
  }
});